package com.zucc.service.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zucc.entity.base.MyMapper;
import com.zucc.util.MyPageUtil;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : charspan
 * @Description :
 * @create : 2021-11-29 4:41 下午
 */
public abstract class MyServiceImpl<T>  {

    public abstract MyMapper<T> mapper();

    public T findById(Long id) {
        return this.mapper().selectByPrimaryKey(id);
    }

    /**
     * TODO 最好在物资查询中重写
     * @param ids
     * @return
     */
    public List<T> findByIds(List<Long> ids) {
        List<T> list = new ArrayList<>();
        ids.forEach(id -> {
            list.add(this.mapper().selectByPrimaryKey(id));
        });
        return list;
    }

    public List<T> findAll() {
        return this.mapper().selectAll();
    }

    public List<T> findByExample(Example e) {
        return this.mapper().selectByExample(e);
    }

    public T findOne(Example e) {
        return this.mapper().selectOneByExample(e);
    }

    public Integer findCount(Example e) {
        return this.mapper().selectCountByExample(e);
    }

    public Integer save(T entity) {
        return this.mapper().insert(entity);
    }

    public Integer saveSelective(T entity) {
        return this.mapper().insertSelective(entity);
    }

    public Integer batchSave(List<T> list) {
        return this.mapper().insertList(list);
    }

    public Integer saveKey(T entity) {
        return this.mapper().insertUseGeneratedKeys(entity);
    }

    public Integer del(Long id) {
        return this.mapper().deleteByPrimaryKey(id);
    }

    public Integer delByExample(Example e) {
        return this.mapper().deleteByExample(e);
    }

    public Integer update(T entity) {
        return this.mapper().updateByPrimaryKey(entity);
    }

    public Integer updateSelective(T entity) {
        return this.mapper().updateByPrimaryKeySelective(entity);
    }

    public Integer update(T t, Example e) {
        return this.mapper().updateByExample(t, e);
    }

    public Integer updateSelective(T t, Example e) {
        return this.mapper().updateByExampleSelective(t, e);
    }

    public PageData<T> findByPage(PageNum num, Example e) {
        PageHelper.startPage(num.getPageNum(), num.getPageSize());
        List<T> ts = this.mapper().selectByExample(e);
        PageInfo<T> p = new PageInfo<>(ts);
        PageData<T> ret = new PageData<>(num.getPageNum(), num.getPageSize(), p.getTotal(), p.getList());
        return ret;
    }

    public PageData<T> findByPage(PageNum num) {
        PageHelper.startPage(num.getPageNum(), num.getPageSize());
        List<T> ts = this.mapper().selectAll();
        PageInfo<T> p = new PageInfo<>(ts);
        PageData<T> ret = new PageData<>(p.getPageNum(), p.getPageSize(), p.getTotal(), p.getList());
        return ret;
    }

    public PageData page(List<T> list, Class cls) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        PageData<T> pageData = new PageData<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
        return MyPageUtil.convert(pageData, cls);
    }
}
